package cn.ecut.controller;


import cn.ecut.model.Loudong;
import cn.ecut.model.Message;
import cn.ecut.model.Taofang;
import cn.ecut.model.Xiangmu;
import cn.ecut.service.Taofangservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/taofang")
public class TaofangController {

    @Autowired
    private Taofangservice taofangservice;

    @ResponseBody
    @RequestMapping("/show")
    public HashMap show() {
        HashMap<String, Object> map = new HashMap<String, Object> ();
        List<Taofang> list = new ArrayList<>();
        list = taofangservice.selectAll();
        Integer count = taofangservice.selectAllcount();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", list);//dao层查询返回list数据
        Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, Object> entry = entries.next();
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/add")
    public Message add(Taofang taofang){
        Message message = new Message();
        Loudong loudong = new Loudong();
        loudong = taofangservice.selectLoudong(taofang.getLoudongbianma());
        taofang.setLoudong(loudong.getLoudong());
        taofang.setLoudongshiyongmianji(loudong.getShiyongmianji());
        taofang.setSuoshuquyu(loudong.getQuyu());
        taofang.setTaofangbianma(taofangservice.gettaofangBianma());
        taofang.setShiyongzhuangtai(1);
        int n = taofangservice.insert(taofang);
        if(n == 1){
            message.setFlag(true);
            return message;
        }else {
            message.setFlag(false);
            return message;
        }
    }

    @ResponseBody
    @RequestMapping("/update")
    public Message update(Taofang taofang){
        Message message = new Message();
        int n =taofangservice.updateByPrimaryKey(taofang);
        if(n == 1){
            message.setFlag(true);
            return message;
        }else {
            message.setFlag(false);
            return message;
        }
    }

    @ResponseBody
    @RequestMapping("/qiyong")
    public Message qiyong(Taofang taofang){
        Message message = new Message();
        System.out.println(taofang.getTaofangbianma());
        if(taofang.getQiyongzhuangtai()==0){
            taofang.setQiyongzhuangtai(1);
        }else {
            taofang.setQiyongzhuangtai(0);
        }
        int n = taofangservice.updateQiyong(taofang);
        if(n == 1){
            message.setFlag(true);
            return message;
        }else {
            message.setFlag(false);
            return message;
        }
    }

    @ResponseBody
    @RequestMapping("/select")
    public HashMap select(String searchResult){
        HashMap<String, Object> map = new HashMap<String, Object> ();
        List<Taofang> list = new ArrayList<>();
        list = taofangservice.advanceSelect(searchResult);
        Integer count = list.size();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", list);//dao层查询返回list数据
        Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, Object> entry = entries.next();
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/delete")
    public Message delete(String taofangbianma){
        Message message = new Message();
        int n = taofangservice.deleteByPrimaryKey(taofangbianma);
        if(n == 1){
            message.setFlag(true);
            return message;
        }else {
            message.setFlag(false);
            return message;
        }
    }
}
